package com.lion.queue.runner;

import java.util.HashMap;
import java.util.Map;

/**
 * 队列管理类
 * 
 * @author liyongyao
 *
 */
public class QueueManager {
	private static QueueManager queueManager;

	private Map<String, Queue<?>> queueMap = new HashMap<String, Queue<?>>();

	@SuppressWarnings("unchecked")
	public <T> Queue<T> createQueue(String queueName, int maxSize,
			Class<T> clazz) {
		queueMap.put(queueName, new Queue<T>(maxSize));
		return (Queue<T>) queueMap.get(queueName);
	}

	public <T> Queue<T> createQueue(String queueName, Class<T> clazz) {
		return createQueue(queueName, 0, clazz);
	}

	/**
	 * 获取队列(不存在时自动创建)
	 * 
	 * @param queueName
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T> Queue<T> getQueue(String queueName, Class<T> clazz) {
		Queue<T> q = (Queue<T>) queueMap.get(queueName);
		if (q == null) {
			return createQueue(queueName, clazz);
		} else {
			return (Queue<T>) queueMap.get(queueName);
		}
	}

	/**
	 * 获取单例
	 * 
	 * @return
	 */
	public static QueueManager getQueueManager() {
		if (queueManager == null) {
			queueManager = new QueueManager();
		}
		return queueManager;
	}

	private QueueManager() {

	}

}
